<template>
  <div>
    <Row>
      <Col span="24">
        <Card class="card clear-float">
          <p class="card-title" slot="title">{{$t('68rVlquTdzxIAxWHi7N6t')}}</p>
          <price-form-info :status="status" :data="data" @save="saveHandler" @del="delHandler"></price-form-info>
        </Card>
      </Col>
    </Row>
  </div>
</template>

<script>
import { getStationPriceInfo } from '@/api/data2'
import PriceFormInfo from './components/price_form_info'

export default {
  name: 'station_price_info',
  components: {
    PriceFormInfo
  },
  data () {
    return {
      data: {},
      status: 'update'
    }
  },
  created () {

  },
  async mounted () {
    await this.queryInfo()
  },
  methods: {
    initData () {
      const forms = {}
      Array.from({ length: 48 }).forEach((v, i) => {
        forms['rate' + (i < 10 ? '0' + i : i)] = '0'
      })
      const obj = {
        stationid: this.$store.state.user.stationId,
        cuspelectprice: '0',
        cuspserviceprice: '0',
        peakelectprice: '0',
        peakserviceprice: '0',
        flatelectprice: '0',
        flatserviceprice: '0',
        valleyelectprice: '0',
        valleyserviceprice: '0',
        ...forms
      }
      return obj
    },
    transform (data) {
      const obj = {}
      _.forEach(data, (v, k) => {
        if (v === '') {
          obj[k] = '0'
        } else {
          obj[k] = v + ''
        }
      })
      return obj
    },
    async queryInfo () {
      const { code, data, desc } = await getStationPriceInfo({
        stationid: this.$store.state.user.stationId
      })
      if (code == 200) {
        this.data = this.transform(data || {})
      } else {
        if (desc === '没有找到指定的电价信息') {
          this.status = 'create'
          this.data = this.initData()
        } else {
          this.$Message.error(desc)
        }
      }
    },
    saveHandler () {
      this.queryInfo()
    },
    delHandler () {
      this.queryInfo()
    }
  }
}
</script>
